import 'dart:async';
import 'dart:ui';

import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart'; //ios库
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';

import '../../http/bean/clockin/attendances.dart';

var _radius = Radius.circular(10);

//判断弹窗是否已经显示出来，防止多次重复打开。
var _isShow = false;
ScrollController? _scrollController = null;
double _offset = 0.0;

///班组选择弹窗
Future<MyGroupBcInfo?>? showDialogWithClass<bool>(
    @required BuildContext? context,
    {List<MyGroupBcInfo>? content,
    String? value}) {
  if (_isShow && context != null) {
    Navigator.of(context).pop(null); //关闭对话框;返回空null
    _isShow = false;
  }
  if (content == null) {
    return null;
  }
  var maxHeight = 500.0;
  var maxWidth = 400.0;
  if (PlatformUtils.isIOS() || PlatformUtils.isAndroid() || PlatformUtils.isFuchsia()) {
    maxHeight = px.x(1500);
    maxWidth = px.x(960);
  }

  List<Widget> children = [];
  content?.forEach((element) {
    if (element != null) {
      Container container;
      if (value == element.GroupId) {
        //选中
        Text text = Text(
          element.GroupName,
          style: TextStyle(color: Colors.white, fontSize: px.x(48)),
        );
        container = Container(
            child: text,
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 10, top: 10, bottom: 5),
            padding: EdgeInsets.only(
                top: 10, bottom: 10, left: px.x(50), right: px.x(50)),
            decoration: BoxDecoration(
              color: ColorUtils.parseColor("#408BF1"),
              borderRadius: BorderRadius.all(Radius.circular(10)),
            ));
      } else {
        Color _color = ColorUtils.parseColor("#cccccc");
        // if (StringUtils.isEmpty(element.Attendances)) {
        //   _color = Colors.red; //班次信息为空。fixme 现在所有班组返回的班次基本都是空的；班次需要根据班组请求二次接口。
        // }
        Text text = Text(
          element.GroupName,
          style: TextStyle(color: Colors.black, fontSize: px.x(48)),
        );
        container = Container(
            child: text,
            alignment: Alignment.center,
            margin: EdgeInsets.only(left: 10, top: 10, bottom: 5),
            padding: EdgeInsets.only(
                top: 10, bottom: 10, left: px.x(50), right: px.x(50)),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10)),
              border: Border.all(color: _color, width: 1),
            ));
      }
      var inkWell = InkWell(
        child: container,
        onTap: () {
          if (_scrollController != null) {
            _offset = _scrollController!.offset;
            _scrollController!.dispose();
            _scrollController = null;
          }
          Navigator.of(context!).pop(element);
        },
      );
      children.add(Row(
        children: [inkWell],
        mainAxisSize: MainAxisSize.min,
      ));
      //children.add(text);
    }
  });
  _scrollController =
      ScrollController(initialScrollOffset: _offset, keepScrollOffset: true);

  ///showCupertinoDialog(context: context, builder: builder)
  ///showDialog
  ///fixme 这里showDialog<bool>必须指明具体的泛型，不然点击确定按钮无效。
  var dialog = showDialog<MyGroupBcInfo?>(
      context: context!,
      builder: (coontext) {
        return AlertDialog(
          title: Container(
            child: Text(AppStrings.of(context).chooseTeam),
            alignment: Alignment.center,
          ),
          titlePadding: EdgeInsets.only(top: 12, bottom: 12),
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.all(_radius)),
          contentPadding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
          scrollable: true,
          content: ConstrainedBox(
            constraints: BoxConstraints(
                minWidth: 100, //最小宽度
                minHeight: 20.0, //最小高度(亲测20的效果刚刚好)
                maxHeight: maxHeight, //最大高度
                maxWidth: maxWidth //最大跨度，double.infinity 宽度尽可能大
                ),
            child: Container(
              color: ColorUtils.parseColor("#F4F4F4"),
              child: Scrollbar(
                child: SingleChildScrollView(
                  controller: _scrollController,
                  child: Wrap(
                    spacing: 0,
                    //水平间距
                    runSpacing: 0,
                    //垂直间距
                    alignment: WrapAlignment.start,
                    //主轴对齐方式
                    crossAxisAlignment: WrapCrossAlignment.start,
                    //交叉轴对齐方式。
                    children: children,
                  ),
                ),
              ),
            ),
          ),
          actionsPadding: EdgeInsets.zero,
          insetPadding: EdgeInsets.zero,
          //titlePadding: EdgeInsets.zero,
          buttonPadding: EdgeInsets.zero,
          actions: <Widget>[
            Column(
              children: [
                Divider(
                  height: 0.5,
                  color: Colors.black12,
                  thickness: 0.5,
                ),
                MaterialButton(
                  splashColor: Colors.black12,
                  highlightColor: Colors.black12,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.only(
                          bottomLeft: _radius, bottomRight: _radius)),
                  height: 50,
                  minWidth: maxWidth,
                  child: Text(
                    LibStrings.of(context).cancel,
                    style: TextStyle(
                        color: ColorUtils.parseColor("#636363"), fontSize: px.x(40)),
                  ),
                  //取消
                  onPressed: () {
                    // ... 执行删除操作
                    Navigator.of(context).pop(null); //关闭对话框;fixme 会返回true
                  },
                ),
              ],
            )
          ],
        );
      });
  // .then((value) => print('弹窗返回：\t$value'));
  // return dialog as Future<bool>;
  _isShow = true; //弹窗已经显示。
  Completer<MyGroupBcInfo?> completer = new Completer();
  (dialog as Future<MyGroupBcInfo?>).then((value) {
    _isShow = false; //弹窗已经关闭
    completer.complete(value);
  });
  return completer.future; //会挂起，这里会等待completer.complete（）执行完毕之后再返回。
}
